//
//  ST_TianDailyTaskCell.swift
//  btvideo
//
//  Created by Jason on 2021/10/27.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
//import Rswift
import SwifterSwift

class ST_TianDailyTaskCell: UICollectionViewCell {
    
    override init(frame: CGRect){
        super.init(frame: frame)
        
        contentView.cornerRadius = 10
        contentView.backgroundColor = UIColor(hex: 0xFFF9E7)
        
        contentView.addSubview(coverImageBtn)
        
        coverImageBtn.snp.makeConstraints { make in
            make.width.height.equalTo(40)
            make.left.top.equalToSuperview().offset(14)
        }
        
        contentView.addSubview(titleLabel)
        
        titleLabel.snp.makeConstraints { make in
            make.height.equalTo(20)
            make.top.equalToSuperview().offset(14)
            make.right.equalToSuperview().offset(-105)
            make.left.equalToSuperview().offset(64)
        }
        
        contentView.addSubview(subtitleLabel)
        
        subtitleLabel.snp.makeConstraints { make in
            make.height.equalTo(18)
            make.bottom.equalToSuperview().offset(-14)
            make.right.equalToSuperview().offset(-105)
            make.left.equalToSuperview().offset(64)
        }
        
        contentView.addSubview(drawBtn)
        drawBtn.snp.makeConstraints { make in
            make.width.equalTo(86)
            make.height.equalTo(32)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-14)
        }
        
        
        contentView.addSubview(rewardView)
        rewardView.snp.makeConstraints { make in
            make.height.equalTo(30)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-14)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var coverImageBtn:UIButton = {
        let btn = UIButton.init(type: UIButton.ButtonType.custom)
        btn.isUserInteractionEnabled = false
        btn.cornerRadius = 20
        return btn
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let l = UILabel()
        l.textColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
        l.font = .systemFont(ofSize: 14, weight: .semibold)
        return l
    }()
    
    fileprivate lazy var subtitleLabel:UILabel = {
        let l = UILabel()
        l.textColor = #colorLiteral(red: 0.6745098039, green: 0.7058823529, blue: 0.7529411765, alpha: 1)
        l.font = .systemFont(ofSize: 13, weight: .regular)
        return l
    }()
    
    
    fileprivate lazy var drawBtn:UIButton = {
        let btn = UIButton.init(type: UIButton.ButtonType.custom)
        btn.cornerRadius = 16
        btn.setTitleColor(.white, for: .normal)
        btn.setTitleColor(.white, for: .disabled)
        btn.setBackgroundImage(UIImage(color: UIColor(hex: 0xF33B3D)!, size: CGSize(width: 1, height: 1)), for: .normal)
        btn.setBackgroundImage(UIImage(color: UIColor(hex: 0xACB4C0)!, size: CGSize(width: 1, height: 1)), for: .disabled)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .semibold)
        btn.alpha = 0
        if let user = ST_BTUserInfoManager.shared.current_account_info?.userinfo,user.sex == .female{
            btn.setTitle("领取积分", for: .normal)
        }else{
            btn.setTitle("领取红包", for: .normal)
        }
        btn.setTitle("今日已领", for: .disabled)
        btn.isUserInteractionEnabled = false
        
        return btn
    }()
    
    fileprivate lazy var rewardView:ST_MissionRewardView = {
        let l = ST_MissionRewardView()
        l.isUserInteractionEnabled = false
        l.alpha = 0
        return l
    }()
    
    func requestLoading(value:Bool = false){
        drawBtn.loading(status: value)
    }
    
    var model:ST_TianDailyTaskCellModel?{
        didSet{
            
            guard let m = model else { return  }
            
            coverImageBtn.pin_setImage(from: m.imageUrl.compress_small_image(width: 40*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            titleLabel.text = m.name
            subtitleLabel.text = m.desc
            
            
            switch m.missionState {
            case .undone(let coin):
                rewardView.alpha = 1
                rewardView.bind(number: coin)
                drawBtn.alpha = 0
            case .done:
                rewardView.alpha = 0
                drawBtn.alpha = 1
                drawBtn.isEnabled = true
            case .received:
                rewardView.alpha = 0
                drawBtn.alpha = 1
                drawBtn.isEnabled = false
            }
        }
    }
}

fileprivate class ST_MissionRewardView:UIControl{
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        [arrow_img_view,icon_image_view,unit_label,value_label].forEach{ addSubview($0) }
        
        arrow_img_view.snp.makeConstraints { make in
            make.right.centerY.equalToSuperview()
            make.width.height.equalTo(16)
        }
        
        icon_image_view.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(arrow_img_view.snp.left).offset(-5)
            make.width.height.equalTo(20)
        }
        
        unit_label.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(icon_image_view.snp.left).offset(-5)
        }
        
        value_label.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(unit_label.snp.left).offset(-1)
            make.left.equalToSuperview()
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var arrow_img_view:UIImageView = {
        let v = UIImageView()
        v.image = ST_ImageUtils.imageName("mission_right_arrow_icon")
        return v
    }()
    
    fileprivate lazy var icon_image_view:UIImageView = {
        let v = UIImageView()
        v.image = ST_ImageUtils.imageName("mission_reward_icon")
        return v
    }()
    
    fileprivate lazy var unit_label:UILabel = {
        let l = UILabel()
        l.textColor = #colorLiteral(red: 0.9529411765, green: 0.231372549, blue: 0.2392156863, alpha: 1)
        l.font = .systemFont(ofSize: 14, weight: .semibold)
        if let user = ST_BTUserInfoManager.shared.current_account_info?.userinfo,user.sex == .female{
            l.text = "积分"
        }else{
            l.text = "币"
        }
        return l
    }()
    
    fileprivate lazy var value_label:UILabel = {
        let l = UILabel()
        l.textColor = #colorLiteral(red: 0.9529411765, green: 0.231372549, blue: 0.2392156863, alpha: 1)
        l.font = .systemFont(ofSize: 16, weight: .semibold)
        return l
    }()
    
    fileprivate func bind(number:String?) {
        if let s = number{
            value_label.text = "+" + s
            value_label.alpha = 1
            unit_label.alpha = 1
        }else{
            value_label.alpha = 0
            unit_label.alpha = 0
        }
    }
    
}



#if PREVIEW
import SwiftUI

struct ST_TianDailyTaskCell_Preview: PreviewProvider {
    static var previews: some View {
        Group{
            
            DebugPreviewView {
                
                let view = ST_TianDailyTaskCell()
                
                view.coverImageBtn.pin_setImage(from: "http://dev-btstatic-cdn.jingniaoapps.com/4C572078-7845-4283-8D7E-0733C0602278".url)
            
                
                view.titleLabel.text = "完成真人认证，我是很长的title测试啦啦啦啦啦绿绿绿绿付"
                
                view.subtitleLabel.text = "添加3个特质，释放超高魅力"
                
//                view.model = .undone(coin: "8")
                
                return view
            }
            .frame(width: 375, height: 80.0)
        }
    }
}
#endif
